﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SMS_Server
{
   public class Function
    {
        public string FunctionName = "";
        public string ParamFormat = "";
        public List<Incoming_Action> Actions = new List<Incoming_Action>();

        public static bool Execute(SMSMessage sms)
        {
            string PhoneNumber = sms.PhoneNo;
            string message = sms.Message;
            message = message.Replace("\n", " ");
            message = message.Trim();
            string functionName = message.Split(' ')[0];
            Function f = null;
            try
            {
                f = Global.Tasks[functionName.ToUpper()];
                if (message.IndexOf(' ') >= 0 && message.IndexOf(' ')<message.Length-1)
                    message = message.Substring(message.IndexOf(' ') + 1);
                else message = "";
                string[] param = message.Split(' ');
                foreach (Incoming_Action ia in f.Actions)
                {
                    string result = "";
                    try
                    {
                        if (ia.GetResult(PhoneNumber, param, out result))
                        {
                            Global.SendSMS(PhoneNumber, result);
                        }
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.EventLog el = new System.Diagnostics.EventLog();
                        el.Log = "Application";
                        el.MachineName = ".";
                        el.Source = "SMS Server";
                        el.WriteEntry("Error executing action: " + ex.Message, System.Diagnostics.EventLogEntryType.Error);

                    }
                }
                Global.IncreamentUsages(functionName);
                return true;
            }
            catch
            {
                try
                {
                    f = Global.Tasks["Unknown"];
                    string[] param = message.Split(' ');
                    foreach (Incoming_Action ia in f.Actions)
                    {
                        string result = "";
                        try
                        {
                            if (ia.GetResult(PhoneNumber, param, out result))
                            {
                                Global.SendSMS(PhoneNumber, result);
                            }
                        }
                        catch (Exception ex)
                        {
                            System.Diagnostics.EventLog el = new System.Diagnostics.EventLog();
                            el.Log = "Application";
                            el.MachineName = ".";
                            el.Source = "SMS Server";
                            el.WriteEntry("Error executing action: " + ex.Message, System.Diagnostics.EventLogEntryType.Error);
                    
                        }
                    }
                    Global.IncreamentUsages("Unknown");
                    return true;
                }
                catch
                {
                    f = null;
                    System.Diagnostics.EventLog el = new System.Diagnostics.EventLog();
                    el.Log = "Application";
                    el.MachineName = ".";
                    el.Source = "SMS Server";
                    el.WriteEntry("Even Unknown function could not be found", System.Diagnostics.EventLogEntryType.Error);
                    return false;
                }
            }
        }

    }
}
